{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python385jvsc74a57bd04c36a90a135e4aa4256adc3692917ccc380701b533777f3da43dc8bc5264a29c",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 第一次作业第4题\n",
    "作者：赵飞\n",
    "学号：202018019427078\n",
    "日期：2021-04-18"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "归一化前的Z：\n [[0.55791314 0.98434129 0.54869185 0.80165033 0.85762614]\n [0.07732393 0.2137558  0.50135998 0.98285073 0.488949  ]\n [0.21958293 0.60177863 0.65709814 0.27622558 0.1135912 ]\n [0.1859513  0.78433719 0.75100732 0.08972277 0.30948443]\n [0.61989001 0.73609996 0.63770815 0.02696324 0.87901754]]\n---------------------------\n归一化后的Z：\n [[0.5545875  1.         0.54495568 0.80917574 0.86764356]\n [0.05260272 0.19510846 0.49551662 0.99844308 0.48255312]\n [0.20119501 0.60040585 0.65818816 0.26035936 0.09048459]\n [0.16606612 0.79109183 0.75627813 0.06555355 0.29509888]\n [0.61932354 0.7407071  0.63793494 0.         0.8899873 ]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "def normalize_matrix(z):\n",
    "    '''\n",
    "    将矩阵或数组归一化,最小的变成0，最大的变成1，最小与最大之间的等比缩放\n",
    "    '''\n",
    "    a = np.max(z)\n",
    "    b = np.min(z)\n",
    "    z = (z - b) / (a - b)\n",
    "    return z\n",
    "    \n",
    "Z = np.random.random((5,5))\n",
    "Z_normalized = normalize_matrix(Z)\n",
    "print(\"归一化前的Z：\\n\", Z)\n",
    "print(\"---------------------------\")\n",
    "print(\"归一化后的Z：\\n\", Z_normalized)"
   ]
  }
 ]
}